package mirrormonkey.framework.entity;

import java.util.Collection;
import java.util.SortedSet;

import mirrormonkey.core.EntityLifecycleListener;
import mirrormonkey.framework.ConnectionInfo;
import mirrormonkey.framework.member.DynamicMemberData;
import mirrormonkey.util.listeners.ListenerConfiguration;

import com.jme3.network.MessageConnection;

public abstract class EntityData {

	protected final int id;

	protected SyncEntity entity;

	private final ListenerConfiguration listenerConfiguration = new ListenerConfiguration();

	protected boolean registered;

	public EntityData(int id, SyncEntity entity) {
		this.id = id;
		this.entity = entity;
		registered = false;
	}

	public SyncEntity getEntity() {
		return entity;
	}

	protected void setEntity(SyncEntity entity) {
		if (this.entity == entity) {
			return;
		}
		if (this.entity != null) {
			this.entity.setData(null);
		}
		this.entity = entity;
		/*
		 * if (entity != null) { entity.setData(this); }
		 */
	}

	public int getId() {
		return id;
	}

	public boolean isRegistered() {
		return registered;
	}

	public boolean isEntityPresent() {
		return entity != null;
	}

	protected abstract void destroy();

	public void addEntityLifecycleListener(EntityLifecycleListener listener) {
		listenerConfiguration.addListener(listener);
	}

	public void removeEntityLifecycleListener(EntityLifecycleListener listener) {
		listenerConfiguration.removeListener(listener);
	}

	public void collectEntityLifecycleListeners(
			SortedSet<EntityLifecycleListener> l) {
		listenerConfiguration.getListeners(EntityLifecycleListener.class, l);
	}

	public ListenerConfiguration getListenerConfiguration() {
		return listenerConfiguration;
	}

	public abstract StaticEntityData getStaticData(
			MessageConnection forConnection);

	public abstract DynamicMemberData[] getMemberData(
			StaticEntityData staticData);

	public abstract Collection<? extends ConnectionInfo> getConnections(
			StaticEntityData staticData);

}
